package com.openapi.service;

import org.apache.log4j.*;

import java.io.IOException;
import java.util.Date;


public class LoggerUtil {

    public static void log(String tagName, String msg) throws IOException {
        Logger logger = Logger.getLogger(LoggerUtil.class);
        Date now = new Date();
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %m%n");
        String date = String.format("/%ty",now)+String.format("/%tm",now)+String.format("/%td/",now);
        String logFilePath = "logs/dataLog" + date + tagName + ".log";
        Appender appender = new FileAppender(layout, logFilePath);
        logger.addAppender(appender);
        logger.info(msg);
        logger.removeAllAppenders();
    }

    public static void logData(String tagName, String msg,String fileName) throws IOException {
        Logger logger = Logger.getLogger(LoggerUtil.class);
        Date now = new Date();
        Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %m%n");
        String date = String.format("/%ty",now)+String.format("/%tm",now)+String.format("/%td/",now);
        String logFilePath = "logs/" + fileName + date + tagName + ".log";
        Appender appender = new FileAppender(layout, logFilePath);
        logger.addAppender(appender);
        logger.info(msg);
        logger.removeAllAppenders();
    }



}
